//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.util.Log;
import com.neurosky.thinkgear.BlinkFilter;
import com.neurosky.thinkgear.FFT;
import com.neurosky.thinkgear.FFTResult;

public class Positivity {
    private BlinkFilter a = new BlinkFilter(512);
    private FFT b = new FFT();
    private FFTResult c;
    private byte d = -56;
    private float[] e;
    private float[] f;
    private float[] g;
    private float[] h;
    private float[] i;
    private float[] j;
    private float[] k;
    private float[] l;
    private float[] m;
    private short n = 5120;
    private short o = 0;
    private short p = 512;
    private double q;
    private int r;
    private double[] s;
    private double[] t;
    private double u;
    private double v;
    private double[] w;
    private double[] x;
    private double y;
    private double z;

    public Positivity() {
        this.e = new float[this.n];
        this.f = new float[this.n];
        this.g = new float[this.n];
        this.h = new float[this.p];
        this.j = new float[this.p];
        this.l = new float[this.p];
        this.m = new float[this.p];
        this.i = new float[this.p];
        this.k = new float[this.p];
        this.s = new double[6];
        this.t = new double[10];
        this.w = new double[6];
        this.x = new double[10];
    }

    public double detect(byte var1, short var2, short var3) {
        if(var1 != this.d) {
            this.e = new float[this.n];
            this.f = new float[this.n];
            this.o = 0;
            return 200.0D;
        } else {
            System.arraycopy(this.e, 1, this.g, 0, this.n - 1);
            this.g[this.n - 1] = (float)var2;
            System.arraycopy(this.g, 0, this.e, 0, this.n);
            System.arraycopy(this.f, 1, this.g, 0, this.n - 1);
            this.g[this.n - 1] = (float)var3;
            System.arraycopy(this.g, 0, this.f, 0, this.n);
            ++this.o;
            if(this.o != this.n) {
                return 200.0D;
            } else {
                Log.v("TGDevice", "Calculating positivity");
                this.o = (short)(this.p * 9);
                this.h = this.a.filter(var1, this.e);
                this.j = this.a.filter(var1, this.f);
                short var6 = this.p;
                int var7 = this.n / this.p;
                float[] var8 = new float[var6];
                this.v = 0.0D;
                this.z = 0.0D;

                for(int var4 = 0; var4 < var7; ++var4) {
                    int var5;
                    for(var5 = 0; var5 < this.p; ++var5) {
                        this.i[var5] = this.h[var4 * this.p + var5];
                        this.k[var5] = this.j[var4 * this.p + var5];
                    }

                    this.c = this.b.calculateFFT(this.i, var8, 1, var6);
                    this.l = this.c.getPower();
                    this.c = this.b.calculateFFT(this.k, var8, 1, var6);
                    this.m = this.c.getPower();

                    for(var5 = 8; var5 <= 13; ++var5) {
                        this.s[var5 - 8] = (double)this.l[var5];
                        this.w[var5 - 8] = (double)this.m[var5];
                    }

                    this.t[var4] = a(this.s);
                    this.x[var4] = a(this.w);
                }

                for(this.r = 0; this.r < this.t.length; ++this.r) {
                    this.v += this.t[this.r];
                    this.z += this.x[this.r];
                }

                this.u = this.v / (double)this.t.length;
                this.y = this.z / (double)this.x.length;
                this.q = (this.y - this.u) / (this.y + this.u) * 100.0D;
                if(this.q > 25.0D) {
                    this.q = 25.0D;
                } else if(this.q < -25.0D) {
                    this.q = -25.0D;
                }

                this.q *= 4.0D;
                return this.q;
            }
        }
    }

    private static double a(double[] var0) {
        double var1 = var0[0];

        for(int var3 = 0; var3 < var0.length; ++var3) {
            if(var0[var3] > var1) {
                var1 = var0[var3];
            }
        }

        return var1;
    }
}
